package com.example.professorevaluation

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.example.professorevaluation.bean.RateBean
import com.example.professorevaluation.dao.ProfessorDatabase
import com.example.professorevaluation.databinding.ActivityRateBinding
import kotlinx.coroutines.launch

class RateActivity : AppCompatActivity() {
    private lateinit var binding: ActivityRateBinding
    private var mName: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRateBinding.inflate(layoutInflater)
        setContentView(binding.root)
        mName = intent.getStringExtra("name")
        setSupportActionBar(binding.toolbar)
        val actionBar = supportActionBar
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true)
            actionBar.title = "Rate:${mName}"
        }
//        binding.svRate.setStarMaxNumber(5)
//        binding.svDifficulty.setStarMaxNumber(5)
//        binding.svRate.set
        binding.tvSubmit.setOnClickListener {
            if (binding.etContent.text.toString().isBlank()){
                Toast.makeText(this, "Review Content isEmpty", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val bean =  RateBean()
            bean.rate = 4
            bean.difficulty = 4
            bean.content = binding.etContent.text.toString()
            bean.name = mName
            lifecycleScope.launch {
                ProfessorDatabase.getInstance(this@RateActivity).evaluationDao().insert(bean)
                setResult(RESULT_OK, Intent())
                finish()
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item)
    }
}